/**
 * Information about the user.
 * 
 * .location            : The GoogleMapLatLng location of the user.
 * .language            : The user's language.
 * 
 * @param callback The callback function. Invoked when user info has been setup.
 */
function UserInfo(callback)
{
	UserInfo.callback = callback;  // Store this for later use.
	
	// Get the location of the user.
	GeoLocation();
	GeoLocation.getCurrentPosition(UserInfo.setPosition, UserInfo.positionError);
}

/**
 * The default location (Greenwich).
 */
UserInfo.DEFAULT_LOCATION = new google.maps.LatLng(51.47879, -0.010677);

/**
 * The default language (English).
 */
UserInfo.DEFAULT_LANGUAGE = "en";

/**
 * Invoked on success obtaining the position.
 * 
 * @param position The geo location position.
 */
UserInfo.setPosition = function(position)
{
	// Set position unknown flag.
    UserInfo.positionUnknown = false;
    
	UserInfo.location = Conversions.geoLocationToGoogleMapLatLng(position);
	UserInfo.location.ajaxApiUsedAsFallback = position.ajaxApiUsedAsFallback;
	
	// Set the language.
	var geocoder = new google.maps.Geocoder();
    geocoder.geocode({latLng: UserInfo.location}, UserInfo.setLanguage);
};

/**
 * Invoked when there was an error obtaining the position.
 */
UserInfo.positionError = function()
{
	// Set position unknown flag.
	UserInfo.positionUnknown = true;
	
	// Set default location.
	UserInfo.location = UserInfo.DEFAULT_LOCATION;
	
	// Set the default language.
    UserInfo.language = UserInfo.DEFAULT_LANGUAGE;
    
    // Notify user info is complete.
	UserInfo.callback();
};

/**
 * Invoked on obtaining a result from the geocode reverse lookup.
 */
UserInfo.setLanguage = function(results, status)
{
	if (status == google.maps.GeocoderStatus.OK)
	{
		// Convert the ccTLD code to ISO639 (country domain to language).
		UserInfo.language = Conversions.ccTLDToISO639(results[results.length-1].address_components[0].short_name);
	}
	else
	{
		// Set the default language.
		UserInfo.language = UserInfo.DEFAULT_LANGUAGE;
	}
	
	// Notify user info is complete.
    UserInfo.callback();
};